package com.ruoyi.industry.controller.back;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industry.domain.ProjectApartment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.industry.domain.ProjectAuction;
import com.ruoyi.industry.service.IProjectAuctionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目-拍卖Controller
 *
 * @author MagicCube
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/auction")
public class ProjectAuctionController extends BaseController
{
    @Autowired
    private IProjectAuctionService projectAuctionService;

    /**
     * 查询项目-拍卖列表
     */
    @RequiresPermissions("industry:auction:list")
    @GetMapping("/list")
    public TableDataInfo list(ProjectAuction projectAuction)
    {
        startPage();
        List<ProjectAuction> list = projectAuctionService.selectProjectAuctionList(projectAuction);
        return getDataTable(list);
    }

    /**
     * 导出项目-拍卖列表
     */
    @RequiresPermissions("industry:auction:export")
    @Log(title = "项目-拍卖", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectAuction projectAuction)
    {
        List<ProjectAuction> list = projectAuctionService.selectProjectAuctionList(projectAuction);
        ExcelUtil<ProjectAuction> util = new ExcelUtil<ProjectAuction>(ProjectAuction.class);
        util.exportExcel(response, list, "项目-拍卖数据");
    }

    /**
     * 导出拍卖委托书列表
     */
    @RequiresPermissions("industry:auction:export")
    @Log(title = "拍卖免费服务委托书", businessType = BusinessType.EXPORT)
    @GetMapping("/entrust")
    public void entrust(HttpServletResponse response, ProjectAuction projectAuction)
    {
        projectAuctionService.exportEntrust(response,projectAuction.getId());
    }

    /**
     * 下载模板
     * @param response response
     * @throws IOException
     */
//    @RequiresPermissions("industry:auction:export")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        List<ProjectAuction> list = new ArrayList<>();
        ProjectAuction projectAuction = ProjectAuction.getAuction();
        list.add(projectAuction);
        ExcelUtil<ProjectAuction> util = new ExcelUtil<>(ProjectAuction.class);
//        util.importTemplateExcel(response,"拍卖管理",list);
        util.exportExcel(response, list, "拍卖数据");
    }

    /**
     * 数据导入
     * @param file 文件
     * @param updateSupport 更新支持
     * @return
     * @throws Exception
     */
    @Log(title = "拍卖管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("industry:auction:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ProjectAuction> util = new ExcelUtil<>(ProjectAuction.class);
        List<ProjectAuction> projectAuctionList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = projectAuctionService.importUser(projectAuctionList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取项目-拍卖详细信息
     */
    @RequiresPermissions("industry:auction:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectAuctionService.selectProjectAuctionById(id));
    }

    /**
     * 新增项目-拍卖
     */
    @RequiresPermissions("industry:auction:add")
    @Log(title = "项目-拍卖", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectAuction projectAuction)
    {
        return toAjax(projectAuctionService.insertProjectAuction(projectAuction));
    }

    /**
     * 修改项目-拍卖
     */
    @RequiresPermissions("industry:auction:edit")
    @Log(title = "项目-拍卖", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectAuction projectAuction)
    {
        return toAjax(projectAuctionService.updateProjectAuction(projectAuction));
    }

    /**
     * 删除项目-拍卖
     */
    @RequiresPermissions("industry:auction:remove")
    @Log(title = "项目-拍卖", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectAuctionService.deleteProjectAuctionByIds(ids));
    }
}
